define(function(require, exports){
	var $ = require('jquery');
	var util = require('util');
	var pubjs = require('pubjs');
	var grid  = require('@base/highGrid');
	var utilDSP = require('modules/common');
	var countSelector = require('modules/countSelector');
	var batchEdit = require('campaign/popwin/batchEdit').main;

	var List = grid.base.extend({
		init: function(config){
			config = pubjs.conf(config, {
					url: 'plan/list',
					reqType: 'websocket',
					param: {
						order: 'impressions|-1,clicks|-1'
					},
					cols: [
						{name:'_id',text:"ID", sort: true, align: 'center', width: 60, format: 'numberFormat'},
						{name:'Name',text:LANG("名称"), type:'index', width: 280, render:'renderName'},
						{name:'StartTime',text:LANG("有效期"), align: 'left', width: 180, render:'renderTime'}
					],
					hasMenu: true,
					hasBatch: true,
					hasSelect: true,
					hasRefresh: true,
					hasAutoRefresh: true,
					default_metrics: pubjs.config('metricsGroup/campaign/metrics'),
					// 在竞价组中有出价
					tab: pubjs.config('metricsGroup/campaign/tabs'),
					subs: [
						'campaign','period'
					],
					gridName: 'plan'
				}
			);

			this.Super('init', arguments);
		},
		renderTime: function(i, val, data, con, tr){
			var begin = data.Start || LANG('不限');
			var end = data.End || LANG('不限');
			var str = begin + LANG(" 至 ") + end;
			return $('<div class="text-truncate"/>').text(str).attr('title', str);
		},
		renderName: function(i, val, data, con, tr){
			if (data._id == 0)
			{
				return data.Name;
			}

			/**
				Status：
				1 正常/进行中
				2 暂停
				3 草稿
				4 未开始
				5 已结束
				6 超预算
			**/
			var icon = [
				['close', LANG('关闭')],
				['running', LANG('进行中')],
				['pause', LANG('已暂停')],
				['draft', LANG('草稿')],
				['notStart ', LANG('未开始')],
				['completed', LANG('已结束')],
				['overBudget', LANG('超预算')],
				['deleted', LANG('已归档')]
			];

			var num = 0;
			var start = data['StartTime'];
			var end = data['EndTime'];
			var now = util.date('Ymd', new Date());
			if (start > now) {
				num =  4;		//未开始
			}
			if (start <= now && (+end === 0 || end >= now) )
			{
				if (data['Exceed']) {
					num =  6;		//超预算
				} else {
					num =  1;		//进行中
				}
			}
			if (+end !== 0 &&  end < now) {
				num =  5;			//已结束
			}

			switch (true)
			{
				case data['Status'] == 2:
					num = 2;
					break;
				case data['Status'] == 3:
					num = 3;
					break;
				case !!data['IsDeleted']:
					num = 7;
					break;
			}

			var status = icon[num];
			// 名称点击跳转编辑
			var editAction = '';
			if(utilDSP.isBrandVersion()){
				editAction = '#serving/plan' + utilDSP.getBrandSearch({'planId': data._id});
			}else{
				editAction = '#plan/edit/' + data._id;
			}

			var name = $('<a class="text-truncate dib"/>').attr({
				// 'target': '_blank',
				'href': editAction
			}).text(LANG(val)).css({
				'vertical-align': 'middle'
			}).width(con.width-45);

			var span = $('<span></span>');
			span.append(name);
			span.css({'line-height': '13px', 'display': 'inline-block'});
			var hasCustomId = !!data.CustomId;
			// 如果有自定义id
			if (hasCustomId)
			{
				span.append($('<div class="fs12 text-muted mt5">'+ data.CustomId +'</div>'));
			}

			var html = $('<div class="tl fl mr10" title="'+ LANG(val) +'"><i title="'+status[1]+'" class="status status-'+status[0]+' mr5"></i>'+'</div>').append(span);
			if (data._id)
			{
				var icon_all = html.find('i').addClass('cur_p');
				this.uiBind(icon_all, 'click', data, this.showBudgetPopwin);

				// 如果有自定义id
				if (hasCustomId)
				{
					icon_all.addClass('mtn15');
				}
			}

			return html;
		},
		showBudgetPopwin: function(ev)
		{
			var data = ev.data;
			var title = LANG('修改预算');
			var type = 'limit';
			var mod = this.$.batchEdit;
			if (!mod)
			{
				mod = this.create('batchEdit', batchEdit, {
					types: {
						limit: {
							module: 'plan/modules/limit.batchEdit',
							url: 'plan/setLimit'
						}
					}
				});
			}
			mod.reset().show().setTitle(title).setType(type).setParam([data._id]).setData(data);
		}
	});

	exports.base = List;

});